<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li
        v-for="(item, index) in sliders"
        :key="item.id"
        class="carousel-item"
        :class="{ fade: activeIndex === index }"
      >
        <RouterLink v-if="item.imgUrl" :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
        <div v-else class="slider">
          <RouterLink v-for="goods in item" :key="goods.id" :to="`/product/${goods.id}`">
            <img :src="goods.picture" alt="">
            <p class="name ellipsis">{{goods.name}}</p>
            <p class="price">&yen;{{goods.price}}</p>
          </RouterLink>
        </div>
      </li>
    </ul>
    <a @click="toggle(-1)" href="javascript:;" class="carousel-btn prev">
      <i class="iconfont icon-angle-left"></i>
    </a>
    <a @click="toggle(1)" href="javascript:;" class="carousel-btn next">
      <i class="iconfont icon-angle-right"></i>
    </a>
    <div class="carousel-indicator">
      <span
        v-for="(item, index) in sliders"
        :key="item.id"
        @click="activeIndex = index"
        :class="{ active: activeIndex === index }"
      ></span>
    </div>
  </div>
</template>

<script>
import { onUnmounted, ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播图数据
    sliders: {
      type: Array,
      default: () => []
    },
    // 是否自动轮播
    autoPlay: {
      type: Boolean,
      default: false
    },
    // 自动轮播时间
    duration: {
      type: Number,
      default: 3000
    }
  },
  setup (props) {
    //   图片对应选中的下标
    const activeIndex = ref(0)
    const toggle = (step) => {
      // 设置temp为变化的选中下标，并对temp进行判定
      const temp = activeIndex.value + step
      //   当下标小于0，让下标变为数组长度减1最大值，并结束函数
      if (temp < 0) {
        activeIndex.value = props.sliders.length - 1
        return
      }
      //   当下标大于数组长度减1最大值时，让下标变为0，并结束函数
      if (temp > props.sliders.length - 1) {
        activeIndex.value = 0
        return
      }
      //   temp 不是极值时赋值回活动的下标
      activeIndex.value = temp
    }
    // 设置timer接收自动轮播ID
    let timer = null
    // 定义自动轮播函数
    const autoPlayFn = () => {
      // 清理自动轮播ID
      clearInterval(timer)
      timer = setInterval(() => {
        // 自动轮播时执行toggle函数
        toggle(1)
        // 自动轮播时间为传进来的时间，默认是3s
      }, props.duration)
    }
    // 侦听传进来的数组
    watch(
      () => props.sliders,
      (newValue) => {
        // newValue形参为侦听的数据
        // console.log(newValue) // props.sliders
        // 当数组的数据大于1并且传进来为true时才能开启自动轮播
        if (newValue.length > 1 && props.autoPlay) {
          autoPlayFn()
        }
        // immediate 为true时表示马上侦听
      },
      { immediate: true }
    )
    // 执行停止函数，鼠标移入时需要执行
    const stop = () => {
      // 清理ID
      clearInterval(timer)
    }
    // 执行开启自动轮播函数，鼠标移出时需要重新开启自动轮播
    const start = () => {
      // 自动轮播开启的条件
      if (props.sliders.length > 1 && props.autoPlay) {
        autoPlayFn()
      }
    }
    // 💥💥💥组件销毁时需要清除轮播，否则不会自动清除
    onUnmounted(() => {
      clearInterval(timer)
    })
    return { activeIndex, toggle, stop, start }
  }
}
</script>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
// 轮播商品
.slider {
  display: flex;
  justify-content: space-around;
  padding: 0 40px;
  > a {
    width: 240px;
    text-align: center;
    img {
      padding: 20px;
      width: 230px!important;
      height: 230px!important;
    }
    .name {
      font-size: 16px;
      color: #666;
      padding: 0 40px;
    }
    .price {
      font-size: 16px;
      color: @priceColor;
      margin-top: 15px;
    }
  }
}
</style>
